import { Engine } from '../engine';
import { DateUtil } from '../../utils/dateUtil';
import { CommonOrderStatus, DocType } from './docDefinition';
import { Util } from '../../utils/util';


/**
 * 各类单据基类
 * todo 添加记录日期，和操作日期
 */
export class DocBase {
    // 单据id
    id = "";

    // 单据title
    title = "";

    // 单据号 (目前就等于id)
    docCode = "";

    // 单据状态
    _status = CommonOrderStatus.WaitReview;

    // 单据类型 DocType
    docType = null;

    // 上级单据类型、code、标题
    parentDocType = null;  //DocType
    parentDocCode = "";
    parentDocTitle = "";

    // 上级单据对象(目前自动化测试在使用)
    parentOrder = null;

    // 源单据单据类型、code、标题
    sourceDocType = null; //DocType
    sourceDocCode = "";
    sourceDocTitle = "";

    // 触发单据单据类型、code、标题
    triggerDocType = null; // DocType
    triggerDocCode = "";
    triggerDocTitle = "";

    // 源单据对象(目前自动化测试在使用)
    sourceOrder = null;

    // 制单人id
    docMakerId = "";

    // 制单人
    docMaker = "";

    // 最近更新人
    _updatorId = "";

    // 最近更新人
    _updator = "";

    // 更新人日期
    _updateDate = null;

    // 单据业务操作人员
    _operator = "";

    // 操作员id
    _operatorId = "";

    // 记录日期 制单日期
    recordDate = null;

    // 操作日期
    _operateDate = null;

    // 所属门店
    storeId = null;

    // 所属门店
    storeName = null;

    // 备注
    remark = "";

    // 获取单据状态
    get status() {
        return this._status;
    }

    // 获取操作员
    get operator() {
        return this._operator;
    }

    // 获取操作员ID
    get operatorId() {
        return this._operatorId;
    }

    // 获取更新人
    get updateor() {
        return this._updator;
    }

    // 获取更新人ID
    get updatorId() {
        return this._updatorId;
    }

    // 获取更新日期
    get updateDate() {
        return this._updateDate;
    }

    // 获取操作日期
    get operateDate() {
        // return DateUtil.getDate(this._operateDate);
        return this._operateDate;
    }

    set operateDate(value) {
        // this._operateDate = DateUtil.getDate(value);
        this._operateDate = value;
    }

    get operateDateString() {
        return this._operateDate == null ? "" : DateUtil.formatDate(this._operateDate,'yyyy-MM-dd HH:mm:ss');
    }

    set operateDateString(value) {
        this.operateDate = DateUtil.strToDate(value);
    }

    // 重置为复制
    resetForCopy({ removeParentAndSourceData = true }) {
        this.id = "";
        this.docCode = "";
        this._status = CommonOrderStatus.WaitReview;
        if (removeParentAndSourceData) {
            this.sourceDocCode = "";
            this.sourceDocTitle = "";
            this.sourceDocType = null;
            this.parentDocCode = "";
            this.parentDocTitle = "";
            this.parentDocType = null;
        }
    }

    // 设置状态为通过
    updateStatusToPassed() {
        this._status = CommonOrderStatus.Passed;
    }

    // 设置经办人
    setOperator({ operator, operatorId }) {
        this._operator = operator;
        this._operatorId = operatorId;
    }

    constructor({
        id,
        title = "",
        docCode = "",
        storeId = null,
        storeName = null,
        status,
        docType,
        parentDocType,
        parentDocCode,
        parentDocTitle,
        sourceDocType,
        sourceDocCode,
        sourceDocTitle,
        docMakerId = "",
        docMaker,
        operator,
        operatorId,
        updator,
        updatorId,
        updateDate,
        recordDateTime = null,
        operateDateTime = null,
        remark = "",
        triggerDocType = null,
        triggerDocCode = "",
        triggerDocTitle = "",
    }) {
        this.id = id;
        this.title = title;
        this.docCode = docCode;
        this._status = status;
        this.docType = docType;
        this.parentDocType = parentDocType;
        this.parentDocCode = parentDocCode;
        this.parentDocTitle = parentDocTitle;
        this.sourceDocType = sourceDocType;
        this.sourceDocCode = sourceDocCode;
        this.sourceDocTitle = sourceDocTitle;
        this.docMakerId = docMakerId;
        this.docMaker = docMaker;
        this._operator = operator;
        this._operatorId = operatorId;
        this._updator = updator;
        this._updatorId = updatorId;
        this._updateDate = updateDate;
        // 修改后（更明确的条件判断）
        this.storeId = (storeId && storeId !== "") ? storeId : Engine.profile?.getValideStore.id;
        this.storeName = (storeName && storeName !== "") ? storeName : Engine.profile?.getValideStore.name;
        this.triggerDocType = triggerDocType;
        this.triggerDocCode = triggerDocCode;
        this.triggerDocTitle = triggerDocTitle;

        // 日期赋值
        this.recordDate = recordDateTime || new Date();
        this._operateDate = operateDateTime || new Date();
        this.remark = remark;
    }

    // 从json转换
    static fromJson(json, type, options = {}) {
        const { parentDocType = null, sourceDocType = null } = options;
        let docBase = new DocBase({
            id: Util.jsonToString(json['id']),
            title: Util.jsonToString(json['orderTitle']),
            docCode: Util.jsonToString(json['docCode']),
            status: json['orderStatus'] == null ? CommonOrderStatus.Unkown : CommonOrderStatus.fromCode(json['orderStatus']),
            docType: type ?? DocType.fromCode(json['orderType']),
            parentDocType: parentDocType ?? DocType.fromCode(json['preOrderType']),
            parentDocCode: Util.jsonToString(json['preOrderId']),
            parentDocTitle: Util.jsonToString(json['parentDocTitle']),
            sourceDocType: sourceDocType ?? DocType.fromCode(json['sourceOrderType']),
            sourceDocCode: Util.jsonToString(json['sourceOrderId']),
            sourceDocTitle: Util.jsonToString(json['sourceOrderTitle']),
            docMakerId: Util.jsonToString(json['createUserId']),
            docMaker: Util.jsonToString(json['createUser']),
            operator: Util.jsonToString(json['handleUserName']),
            operatorId: json['handleUserId'] == null ? "" : json['handleUserId'].toString(),
            remark: Util.jsonToString(json['orderRemark']),
            updator: Util.jsonToString(json['updateUser']),
            updatorId: Util.jsonToString(json['updateUserId']),
            updateDate: json['updateTime'] == null ? null : new Date(json['updateTime']),
            storeId: Util.jsonToString(json['storeId']),
            storeName: Util.jsonToString(json['storeName']),
            triggerDocType: DocType.fromCode(json['triggerOrderType']),
            triggerDocCode: Util.jsonToString(json['triggerOrderId']),
            triggerDocTitle: Util.jsonToString(json['triggerOrderTitle']),
        });

        // 注意
        docBase.docCode = docBase.id;
        docBase.recordDate = json['createTime'] == null ? new Date() : new Date(json['createTime']);
        docBase.operateDate = json['businessTime'] == null ? new Date() : new Date(json['businessTime']);
        return docBase;
    }

    // 复制构造函数
    static clone(doc) {
        return new DocBase({
            id: doc.id,
            title: doc.title,
            docCode: doc.docCode,
            status: doc._status,
            docType: doc.docType,
            parentDocCode: doc.parentDocCode,
            parentDocType: doc.parentDocType,
            parentDocTitle: doc.parentDocTitle,
            sourceDocType: doc.sourceDocType,
            sourceDocCode: doc.sourceDocCode,
            sourceDocTitle: doc.sourceDocTitle,
            docMakerId: doc.docMakerId,
            docMaker: doc.docMaker,
            operator: doc._operator,
            operatorId: doc._operatorId,
            remark: doc.remark,
            recordDate: doc.recordDate,
            updatorId: doc._updatorId,
            updator: doc._updator,
            updateDate: doc._updateDate,
            operateDate: doc._operateDate,
            storeId: doc.storeId,
            storeName: doc.storeName,
            triggerDocType: doc.triggerDocType,
            triggerDocCode: doc.triggerDocCode,
            triggerDocTitle: doc.triggerDocTitle,
        });
    }

    docBaseClone(doc) {
        this.id = doc.id;
        this.title = doc.title;
        this.docCode = doc.docCode;
        this._status = doc._status;
        this.docType = doc.docType;
        this.parentDocCode = doc.parentDocCode;
        this.parentDocType = doc.parentDocType;
        this.parentDocTitle = doc.parentDocTitle;
        this.sourceDocType = doc.sourceDocType;
        this.sourceDocCode = doc.sourceDocCode;
        this.sourceDocTitle = doc.sourceDocTitle;
        this.docMakerId = doc.docMakerId;
        this.docMaker = doc.docMaker;
        this._operator = doc._operator;
        this._operatorId = doc._operatorId;
        this.remark = doc.remark;
        this.recordDate = doc.recordDate;
        this._updatorId = doc._updatorId;
        this._updator = doc._updator;
        this._updateDate = doc._updateDate;
        this._operateDate = doc._operateDate;
        this.storeId = doc.storeId;
        this.storeName = doc.storeName;
        this.triggerDocType = doc.triggerDocType;
        this.triggerDocCode = doc.triggerDocCode;
        this.triggerDocTitle = doc.triggerDocTitle;
    }

    // 转换成json
    toJson() {
        let a = DocType.DCKD;
        a.code
        let retJson = {
            businessTime: DateUtil.getFullDateTime(this.operateDate),
            createUserId: Engine.profile.id,
            createUser: Engine.profile.nickName.isNotEmpty ? Engine.profile.nickName : Engine.profile.userName,
            handleUserId: this.operatorId,
            handleUserName: this.operator,
            id: this.id,
            orderStatus: this.status.code,// CommonOrderStatus.values.indexOf(this.status), 
            orderTitle: this.title,
            orderType: this.docType.code,//DocType.values.indexOf(this.docType),
            orgId: Engine.profile.org.orgId,
            preOrderId: this.parentDocCode,
            orderRemark: this.remark,
            sourceOrderId: this.sourceDocCode,
            sourceOrderType: this.sourceDocType ? this.sourceDocType.code : null,
            updateUserId: Engine.profile.id,
            updateUser: Engine.profile.nickName,
            storeId: this.storeId,
            preOrderId: this.parentDocCode,
        };

        // 如果 sourceOrderType = null 去除该参数
        if (retJson.sourceDocType == null) {
            delete retJson['sourceOrderType'];
        }

        return retJson;
    }
}
